package com.grass.cropTradingCenter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.cropTradingCenter.domain.repository.OrderRepository;
import com.grass.cropTradingCenter.mapper.UserOrderMapper;
import com.grass.cropTradingCenter.pojo.dto.CropDto;
import com.grass.cropTradingCenter.pojo.entity.GoodsPackage;
import com.grass.cropTradingCenter.pojo.entity.ShippingAddress;
import com.grass.cropTradingCenter.pojo.entity.UserOrder;
import com.grass.cropTradingCenter.pojo.vo.user.QueryOrderVo;
import com.grass.cropTradingCenter.pojo.vo.user.SaveOrderVo;
import com.grass.cropTradingCenter.service.UserOrderService;
import com.grass.cropTradingCenter.service.domain.OrderDomainService;
import com.grass.cropTradingCenter.utils.OrderCodeUtils;
import com.grass.cropTradingCenter.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zipan
 * @description 针对表【user_order】的数据库操作Service实现
 * @createDate 2024-04-01 23:39:41
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder>
        implements UserOrderService {

    @Autowired
    OrderDomainService orderDomainService;

    @Autowired
    OrderRepository orderRepository;

    @Override
    public void saveOrder(SaveOrderVo vo) {
        UserOrder order = this.convert(vo);
        orderDomainService.save(order);
    }

    @Override
    public List<UserOrder> getOrderList(QueryOrderVo vo) {
        return orderRepository.getOrderList(vo);
    }

    @Override
    public void confirmSH(Integer id) {
        orderRepository.confirmSH(id);
    }

    private UserOrder convert(SaveOrderVo vo) {
        UserOrder order = new UserOrder();
        ShippingAddress address = vo.getAddress();
        order.setAddressName(address.getAddressName());
        order.setAddressDetail(address.getAddressRegionStr() + " " + address.getAddressDetail());
        order.setAddressPhone(address.getAddressPhone());

        CropDto goods = vo.getGoods();
        order.setGoodsDescription(goods.getGoodsDescription());
        order.setGoodsId(goods.getGoodsId());
        order.setGoodsDisplayUrl(goods.getDisplayUrl());
        order.setGoodsName(goods.getGoodsName());
        order.setShopId(goods.getShopId());
        order.setShopName(goods.getShopName());


        GoodsPackage goodsPackage = vo.getGoodsPackage();
        order.setPackagePrice(goodsPackage.getPrice());
        order.setPackageDescription(goodsPackage.getDescription());

        order.setBuyNum(vo.getNum());
        order.setUserId(SecurityUtils.getUserId());
        order.setTotalPrice(goodsPackage.getPrice().multiply(new BigDecimal(vo.getNum())));
        order.setOrderType(2);
        order.setOrderCode(OrderCodeUtils.getOrderCode(Long.parseLong(Integer.toString(SecurityUtils.getUserId()))));
        return order;
    }
}




